package geektime.spring.springbucks.controller;

import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.service.CoffeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by PC on 2022/6/15.
 */
@RestController
@RequestMapping("/coffee")
@CrossOrigin(origins = "*")
public class CoffeeController {

    @Autowired
    private CoffeeService coffeeService;

    /*
    * 在需要返回的bean 上面使用@XmlRootElement (name="") 这个name 代表这个xml 根节点的名称，例Coffee
    * 根据请求头Accept 来访问 不同格式的Rest API 数据
    * Accept
    * application/xml;charset=UTF-8
    * application/json;charset=UTF-8
    * */

    @RequestMapping(path = "/{name}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Coffee getCoffee(@PathVariable("name") String name) {
        Coffee coffee = new Coffee();
        coffee.setName(name);
        return coffeeService.findCoffeeByName(coffee);
    }

    @RequestMapping(path = "/{name}", method = RequestMethod.GET, produces = {"application/xml;charset=UTF-8"})
    @ResponseBody
    public Coffee getCoffeeXML(@PathVariable("name") String name) {
        Coffee coffee = new Coffee();
        coffee.setName(name);
        return coffeeService.findCoffeeByName(coffee);
    }

    @RequestMapping(path = "/findAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public List<Coffee> findAll() {
        return coffeeService.allCoffees();
    }
}
